<template>
	<view>
		<nav-bar fontColor="#FFFFFF" title="注册"></nav-bar>
		<uni-section title="填写信息" type="line">
			<view class="example">
				<!-- 基础用法，不包含校验规则 -->
				<uni-forms ref="baseForm" :model="form" labelWidth="150px" labelPosition="top">
					<uni-forms-item label="营业执照" required>
						<htz-image-upload :max="1" mediaType="image" name="file" :chooseNum="1" v-model="imageData"
							@chooseSuccess="ceshiChooseSuccess">
						</htz-image-upload>
					</uni-forms-item>
					<uni-forms-item label="法人身份证正反面" required>
						<view style="display: flex;justify-content: center;">
							<htz-image-upload :max="1" mediaType="image" name="file" :chooseNum="1" v-model="imageData1"
								@chooseSuccess="ceshiChooseSuccess1">
							</htz-image-upload>
							<htz-image-upload :max="1" mediaType="image" name="file" :chooseNum="1" v-model="imageData2"
								@chooseSuccess="ceshiChooseSuccess2">
							</htz-image-upload>
						</view>
						
					</uni-forms-item>
					<uni-forms-item label="企业名称" required>
						<view style="display: flex;">
							<zxz-uni-data-select v-model="form.customerID" filterable :localdata="companyList"
								@change="change" @input="changes"></zxz-uni-data-select>
							<!-- <uni-data-select :localdata="companyList" v-model="form.customerID"
								placeholder="请选择企业"></uni-data-select> -->
							<!-- <view @click="newCompany"
								style="padding: 20rpx;font-size: 28rpx;color: #3B5DE0;letter-spacing: 3rpx;font-weight: bold;">
								新增企业</view> -->
						</view>
					</uni-forms-item>
					<uni-forms-item label="统一社会信用代码" required>
						<uni-easyinput v-model="form.uscc" placeholder="请输入统一社会信用代码" />
					</uni-forms-item>
					<uni-forms-item label="成立日期" required>
						<uni-easyinput v-model="form.establishingTime" placeholder="请输入成立日期" />
					</uni-forms-item>
					<uni-forms-item label="注册资本" required>
						<uni-easyinput v-model="form.registerCapital" placeholder="请输入注册资本" />
					</uni-forms-item>
					<uni-forms-item label="企业类型" required>
						<uni-easyinput v-model="form.companyType" placeholder="请输入企业类型" />
					</uni-forms-item>

					<uni-forms-item label="注册地址" required>
						<uni-easyinput v-model="form.registerAddress" placeholder="请输入注册地址" />
					</uni-forms-item>
					<!-- <uni-forms-item label="身份证号" required>
						<uni-easyinput v-model="form.idCard" placeholder="请输入法人身份证号" />
					</uni-forms-item> -->

					<!-- <uni-forms-item v-for="item in Company" label="企业名称">
						<view style="display: flex;">
							<uni-easyinput v-model="form.companyName" placeholder="请输入企业名称" />
							<view @click="deleteCompany"
								style="padding: 20rpx;font-size: 28rpx;color: #ff0000;letter-spacing: 3rpx;font-weight: bold;">
								删除</view>
						</view>
					</uni-forms-item> -->
					<uni-forms-item label="法人姓名" required>
						<uni-easyinput v-model="form.nickName" placeholder="请输入您的姓名" />
					</uni-forms-item>
					<uni-forms-item label="法人身份证号" required>
						<uni-easyinput v-model="form.idCard" placeholder="请输入法人身份证号" />
					</uni-forms-item>
					<uni-forms-item label="身份证有效期" required>
						<uni-easyinput v-model="form.idCardValidTo" placeholder="请输入身份下有效期" />
					</uni-forms-item>
					<uni-forms-item label="手机号" required>
						<view style="display: flex;">
							<uni-easyinput style="width: 35%;" v-model="form.phone" placeholder="请输入手机号" />
							<view v-if="codeStatus" @click="setCountDown"
								style="padding: 20rpx;font-size: 28rpx;color: #3B5DE0;letter-spacing: 3rpx;font-weight: bold;">
								发送验证码</view>
							<view v-else
								style="padding: 20rpx;font-size: 28rpx;color: #b8b8b8;letter-spacing: 3rpx;font-weight: bold;">
								{{count}}
							</view>
						</view>
					</uni-forms-item>
					<uni-forms-item label="验证码" required>
						<view style="display: flex;">
							<uni-easyinput v-model="form.smsCode" placeholder="请输入验证码" />
						</view>
					</uni-forms-item>
					<!-- <uni-forms-item label="登录密码 " required>
						<uni-easyinput v-model="form.passWord" type="password" placeholder="请输入登录密码" />
					</uni-forms-item>
					<uni-forms-item label="确认密码 " required>
						<uni-easyinput v-model="form.confirmPassword" type="password" placeholder="请输入确认密码" />
					</uni-forms-item> -->
				</uni-forms>
			</view>
			<view style="height: 60rpx;">
				<view class="bottomBox">
					<view class="buttonBox">
						<button class="button_fix" @click="submit" size="default">注册</button>
					</view>
				</view>
			</view>
		</uni-section>
	</view>
</template>

<script>
	import htzImageUpload from '@/components/htz-image-upload/htz-image-upload-show.vue'
	import base_url from '@/config/requestConfig.js'
	export default {
		components: {
			htzImageUpload,
		},
		data() {
			return {
				Company: [],
				form: {
					customerID: '',
					companyName: '',
					nickName: '',
					phone: '',
					smsCode: '',
					// passWord: '',
					// confirmPassword: '',
					yyzzUrl: '',
					ownerIdUrl: '',
					uscc: '',
					establishingTime: '',
					registerCapital: '',
					companyType: '',
					idCardValidTo: '',
					idCard: '',
					registerAddress: ''

				},
				companyList: [],
				imageData: [],
				imageData1: [],
				imageData2: [],
				timer: null,
				count: 60,
				codeStatus: true
			}
		},
		onLoad() {
			// this.companySearch();
		},
		methods: {
			setCountDown() {
				if (this.form.phone != '' && this.form.phone.length == 11) {
					this.getSmsCode();
					this.codeStatus = !this.codeStatus
					this.timer = setInterval(() => {
						if (this.count > 0) {
							this.count--;
							console.log(this.count)
						} else {
							this.count = 60;
							this.codeStatus = !this.codeStatus
							clearInterval(this.timer);
						}
					}, 1000)
				} else {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'error'
					})
				}

			},
			getSmsCode() {
				let opt = {
					url: 'getSmsCode/' + this.form.phone,
					method: 'GET'
				};
				this.$http.httpRequest(opt, {}).then(res => {
					if (res.data.code == 200) {
						uni.showToast({
							title: res.data.msg,
							icon: 'success'
						})
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: 'error'
						})
					}
				})
			},
			newCompany(e) {
				this.form.companyName = '';
				this.form.customerID = '';
				if (e) {
					this.form.companyName = e;
				}
				this.companyList = [];
				if (this.Company == '') {
					this.Company.push(1)
				}
			},
			deleteCompany() {
				this.Company = []
				this.form.companyName = '';
			},
			submit() {
				if(this.form.yyzzUrl == ''){
					uni.showToast({
						title:'请上传营业执照',
						icon:'error',
						duration: 1500
					});
					return
				}
				if(this.imageData1.length == ''){
					uni.showToast({
						title:'请上传身份证正反面',
						icon:'error',
						duration: 1500
					});
					return
				}
				if(this.imageData2.length == ''){
					uni.showToast({
						title:'请上传身份证正反面',
						icon:'error',
						duration: 1500
					});
					return
				}

				if (this.form.customerID == '') {
					uni.showToast({
						title: '请选择企业',
						icon: 'error',
						duration: 1500
					});
					return
				}
				if (this.form.phone == '') {
					uni.showToast({
						title: '请输入手机号',
						icon: 'error',
						duration: 1500
					});
					return
				}
				if (this.form.passWord == '') {
					uni.showToast({
						title: '请输入验证码',
						icon: 'error',
						duration: 1500
					});
					return
				}
				let imgData = [];
				imgData.push(this.imageData1[0].slice(35),this.imageData2[0].slice(35))
				this.form.ownerIdUrl = imgData.join(',')
				console.log(this.form.ownerIdUrl)
				let opt = {
					url: 'basic/customer/insertFactoryUser',
					method: 'POST'
				};
				this.$http.httpRequest(opt, this.form).then(res => {
					if (res.data.code == 200) {
						uni.showToast({
							title: res.data.msg,
							icon: 'success',
							duration: 1500
						});
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 1500)
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: 'error',
							duration: 1500
						});
					}
				})
			},
			change(e) {
				this.form.customerID = e
				for (let i in this.companyList) {
					if (e == this.companyList[i].value) {
						this.form.companyName = this.companyList[i].text
					}
				}
				console.log(this.form.customerID, this.form.companyName)
			},
			changes(e) {

				let opt = {
					url: 'basic/customer/search',
					method: 'POST'
				};
				this.$http.httpRequest(opt, {
					customerName: e
				}).then(res => {
					if (res.data.data != '') {
						console.log(res.data.data.length)
						this.deleteCompany();
						for (let i in res.data.data) {
							this.companyList = []
							this.companyList.push({
								text: res.data.data[i].customerName,
								value: res.data.data[i].id
							})
						}
						if (res.data.data.length == 1) {
							this.form.customerID = res.data.data[0].id
							this.form.companyName = res.data.data[0].customerName
							console.log(this.form.customerID, this.form.companyName)
						}
					} else {
						// this.form.customerID = '';
						// this.form.customerName = '';
						// uni.showToast({
						// 	title: '无该企业，请新增！',
						// 	icon:'exception',
						// 	duration: 1500
						// });
						// setTimeout(()=>{
						// 	this.newCompany(e)
						// },1500)
					}
				})
			},
			companySearch() {
				let opt = {
					url: 'basic/customer/search',
					method: 'POST'
				};
				this.$http.httpTokenRequest(opt, {
					customerName: '图木舒克市文明茶庄'
				}).then(res => {
					for (let i in res.data.data) {
						this.companyList.push({
							text: res.data.data[i].customerName,
							value: res.data.data[i].id
						})
					}
				})
			},
			ceshiChooseSuccess(tempFilePaths, e) { //选择图片返回
				console.log('ceshiChooseSuccess', tempFilePaths, e);
				// this.imageData=this.imageData.concat(tempFilePaths)

				//this.imageData.push(...tempFilePaths)
				/****************
				以下代码是自定义上传逻辑，仅供参考
				***************/
				// this.tempFilePaths = tempFilePaths
				// this.imageData.push(tempFilePaths[0]);
				this.imgUpload(tempFilePaths);
				console.log(tempFilePaths)
				/*******************************/
			},
			ceshiChooseSuccess1(tempFilePaths, e) { //选择图片返回
				console.log('ceshiChooseSuccess', tempFilePaths, e);
				// this.imageData=this.imageData.concat(tempFilePaths)

				//this.imageData.push(...tempFilePaths)
				/****************
				以下代码是自定义上传逻辑，仅供参考
				***************/
				// this.tempFilePaths = tempFilePaths
				// this.imageData.push(tempFilePaths[0]);
				this.imgUpload1(tempFilePaths);
				console.log(tempFilePaths)
				/*******************************/
			},
			ceshiChooseSuccess2(tempFilePaths, e) { //选择图片返回
				console.log('ceshiChooseSuccess', tempFilePaths, e);
				// this.imageData=this.imageData.concat(tempFilePaths)
			
				//this.imageData.push(...tempFilePaths)
				/****************
				以下代码是自定义上传逻辑，仅供参考
				***************/
				// this.tempFilePaths = tempFilePaths
				// this.imageData.push(tempFilePaths[0]);
				this.imgUpload2(tempFilePaths);
				console.log(tempFilePaths)
				/*******************************/
			},
			imgUpload(tempFilePaths) {
				if (!tempFilePaths) {
					uni.showToast({
						title: '请上传照片！',
						position: 'center',
						icon: 'error'
					});
					return
				}
				uni.showLoading({
					title: '上传中'
				});
				console.log('imgUpload', tempFilePaths)
				let uploadImgs = [];
				tempFilePaths.forEach((item, index) => {
					uploadImgs.push(new Promise((resolve, reject) => {
						console.log(index, item)
						uni.uploadFile({
							url: base_url.baseUrl + 'system/attachment/upload', //仅为示例，非真实的接口地址
							// url: "http://localhost:18527/system/attachment/upload",
							filePath: item,
							// header:{
							// 	'Authorization':'Bearer ' + uni.getStorageSync('token')
							// },
							name: 'businessLicense',
							success: (uploadFileRes) => {
								console.log('uploadFileRes', uploadFileRes)
								resolve(uploadFileRes);
								var _res = JSON.parse(uploadFileRes.data);
								if (_res.code == 200) {
									this.imageData.push(base_url.fileUrl + 'attach/' + _res
										.data[0].fileUrl);
									this.parseImage1(tempFilePaths)
								} else {
									uni.showToast({
										title: res.data.msg,
										icon: 'error',
										duration: 1500
									})
								}
							},
							fail: (err) => {
								console.log(err);
								reject(err);
							},
							complete: () => {
								//uni.hideLoading();
							}
						});
					}))
				})
				Promise.all(uploadImgs) //执行所有需请求的接口
					.then((results) => {
						uni.hideLoading();
					})
					.catch((res, object) => {
						uni.hideLoading();
					});
			},
			parseImage1(tempFilePaths) {
				console.log('imgUpload', tempFilePaths)
				let uploadImgs = [];
				tempFilePaths.forEach((item, index) => {
					uploadImgs.push(new Promise((resolve, reject) => {
						console.log(index, item)
						uni.uploadFile({
							url: base_url.baseUrl + 'tools/parseImage', //仅为示例，非真实的接口地址
							// url: "http://localhost:18527/system/attachment/upload",
							filePath: item,
							// header:{
							// 	'Authorization':'Bearer ' + uni.getStorageSync('token')
							// },
							name: 'businessLicense',
							success: (uploadFileRes) => {
								console.log('uploadFileRes', uploadFileRes)
								resolve(uploadFileRes);
								var _res = JSON.parse(uploadFileRes.data);
								if (_res.code == 200) {
									if (_res.data) {
										this.form.yyzzUrl = _res.data.fileUrl
										this.form.registerAddress = _res.data.address
										this.form.uscc = _res.data.registrationNumber
										this.form.establishingTime = _res.data.foundDate
										this.form.registerCapital = _res.data.registeredCapital
										this.form.companyType = _res.data.type
										this.changes(_res.data.name)
									} else {
										uni.showToast({
											title: '请上传正确照片',
											icon: 'error',
											duration: 1500
										})
									}
								}
							},
							fail: (err) => {
								console.log(err);
								reject(err);
							},
							complete: () => {
								//uni.hideLoading();
							}
						});
					}))
				})
				Promise.all(uploadImgs) //执行所有需请求的接口
					.then((results) => {
						uni.hideLoading();
					})
					.catch((res, object) => {
						uni.hideLoading();
					});
			},
			imgUpload1(tempFilePaths) {
				if (!tempFilePaths) {
					uni.showToast({
						title: '请上传照片！',
						position: 'center',
						icon: 'error'
					});
					return
				}
				uni.showLoading({
					title: '上传中'
				});
				console.log('imgUpload', tempFilePaths)
				let uploadImgs = [];
				tempFilePaths.forEach((item, index) => {
					uploadImgs.push(new Promise((resolve, reject) => {
						console.log(index, item)
						uni.uploadFile({
							// url: "http://115.29.199.1:18527/system/attachment/upload", //仅为示例，非真实的接口地址
							url: base_url.baseUrl + 'system/attachment/upload',
							filePath: item,
							// header:{
							// 	'Authorization':'Bearer ' + uni.getStorageSync('token')
							// },
							name: 'idCard',
							success: (uploadFileRes) => {
								console.log('uploadFileRes', uploadFileRes)
								resolve(uploadFileRes);
								var _res = JSON.parse(uploadFileRes.data);
								if (_res.code == 200) {
									console.log(_res.data[0].fileUrl)
									this.imageData1.push(
										base_url.fileUrl + 'attach/' +
										_res.data[0].fileUrl);
										this.parseImage2(tempFilePaths)
								}
								else {
									uni.showToast({
										title: res.data.msg,
										icon: 'error',
										duration: 1500
									})
								}
							},
							fail: (err) => {
								console.log(err);
								reject(err);
							},
							complete: () => {
								//uni.hideLoading();
							}
						});
					}))
				})
				Promise.all(uploadImgs) //执行所有需请求的接口
					.then((results) => {
						uni.hideLoading();
					})
					.catch((res, object) => {
						uni.hideLoading();
					});
			},
			imgUpload2(tempFilePaths) {
				if (!tempFilePaths) {
					uni.showToast({
						title: '请上传照片！',
						position: 'center',
						icon: 'error'
					});
					return
				}
				uni.showLoading({
					title: '上传中'
				});
				console.log('imgUpload', tempFilePaths)
				let uploadImgs = [];
				tempFilePaths.forEach((item, index) => {
					uploadImgs.push(new Promise((resolve, reject) => {
						console.log(index, item)
						uni.uploadFile({
							// url: "http://115.29.199.1:18527/system/attachment/upload", //仅为示例，非真实的接口地址
							url: base_url.baseUrl + 'system/attachment/upload',
							filePath: item,
							// header:{
							// 	'Authorization':'Bearer ' + uni.getStorageSync('token')
							// },
							name: 'idCard',
							success: (uploadFileRes) => {
								console.log('uploadFileRes', uploadFileRes)
								resolve(uploadFileRes);
								var _res = JSON.parse(uploadFileRes.data);
								if (_res.code == 200) {
									console.log(_res.data[0].fileUrl)
									this.imageData2.push(
										base_url.fileUrl + 'attach/' +
										_res.data[0].fileUrl);
										this.parseImage2(tempFilePaths)
								}
								else {
									uni.showToast({
										title: res.data.msg,
										icon: 'error',
										duration: 1500
									})
								}
							},
							fail: (err) => {
								console.log(err);
								reject(err);
							},
							complete: () => {
								//uni.hideLoading();
							}
						});
					}))
				})
				Promise.all(uploadImgs) //执行所有需请求的接口
					.then((results) => {
						uni.hideLoading();
					})
					.catch((res, object) => {
						uni.hideLoading();
					});
			},
			parseImage2(tempFilePaths) {
				console.log('imgUpload', tempFilePaths)
				let uploadImgs = [];
				tempFilePaths.forEach((item, index) => {
					uploadImgs.push(new Promise((resolve, reject) => {
						console.log(index, item)
						uni.uploadFile({
							url: base_url.baseUrl + 'tools/parseImage', //仅为示例，非真实的接口地址
							// url: "http://localhost:18527/system/attachment/upload",
							filePath: item,
							// header:{
							// 	'Authorization':'Bearer ' + uni.getStorageSync('token')
							// },
							name: 'idCard',
							success: (uploadFileRes) => {
								console.log('uploadFileRes', uploadFileRes)
								resolve(uploadFileRes);
								var _res = JSON.parse(uploadFileRes.data);
								if (_res.code == 200) {
									if (_res.data) {
										if (_res.data.name) {
											this.form.nickName = _res.data.name
										}
										if (_res.data.number) {
											this.form.idCard = _res.data.number
										}
										if (_res.data.validFrom) {
											this.form.idCardValidTo = _res.data.validFrom + '-' + _res.data.validTo
										}
									} else {
										uni.showToast({
											title: '请上传正确照片',
											icon: 'error',
											duration: 1500
										})
									}
								}
							},
							fail: (err) => {
								console.log(err);
								reject(err);
							},
							complete: () => {
								//uni.hideLoading();
							}
						});
					}))
				})
				Promise.all(uploadImgs) //执行所有需请求的接口
					.then((results) => {
						uni.hideLoading();
					})
					.catch((res, object) => {
						uni.hideLoading();
					});
			}
		}
	}
</script>

<style>
	.example {
		padding: 20rpx;
		background: #fff;
	}

	.bottomBox {
		position: fixed;
		width: 100%;
		bottom: 0;
		margin-bottom: 20rpx;
	}

	.buttonBox {
		width: 100%;
		margin: 0 auto;
		display: flex;
		justify-content: space-around;

		// margin-top: 200rpx;
	}

	.button_fix {
		width: 90%;
		height: 70rpx;
		// margin: 0 auto;
		color: #FFFFFF;
		background: #2749C8;
		font-weight: bold;
		letter-spacing: 5rpx;
		font-size: 28rpx;
		line-height: 70rpx;

	}
</style>